package com.motion.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.motion.dto.UserTaskDto;
import com.motion.dto.UserTrainDto;
import com.motion.generator.entity.Task;
import com.motion.generator.entity.UserTask;
import com.motion.generator.mapper.TaskMapper;
import com.motion.generator.mapper.UserTaskMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TaskService {
    @Autowired(required = false)
    public TaskMapper taskMapper;
    @Autowired(required = false)
    public UserTaskMapper userTaskMapper;
    public List<Task> getAllTaskList(){
        return taskMapper.selectList(new QueryWrapper<>());
    }
    public List<UserTaskDto> taskList2UserTrainDto(List<Task> taskList,Long useId){
        List<UserTaskDto> ref=new ArrayList<>();
        UserTaskDto tem;
        for (Task item:taskList){
            tem=new UserTaskDto();
            BeanUtils.copyProperties(item,tem);
            if(userTaskMapper.selectOne(new QueryWrapper<UserTask>().eq("userId",useId).eq("taskId",item.getTaskId()))!=null){
                tem.setIfTaskFinish(1);
            }else{
                tem.setIfTaskFinish(0);
            }
            ref.add(tem);
        }
        return ref;
    }
    public Task insertTask(Task task){
        taskMapper.insert(task);
        return task;
    }
}
